package com.xnyzc.lhy.didi.config;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * app推送的配置与具体方法
 */
@Slf4j
@Configuration
@EnableCaching
public class AppPushConfig {

    // STEP1：获取应用基本信息
    private static String appId = "3y6D8Ivp4x7vN02xlrmqE3";
    private static String appKey = "aqc5v0d30H6VZZnTFJYpY";
    private static String AppSecret = "c0uiGQFjcC79pwRJVI8Oh7";
    private static String masterSecret = "4fZ7SsR2zg8WjD1C1Vv0b8";
    // 如果需要使用HTTPS，直接修改url即可
    //private static String url = "https://api.getui.com/apiex.htm";
    private static String url = "http://api.getui.com/apiex.htm";

    /**
     * app推送具体方法
     * @param orderNo
     * @return
     * @throws IOException
     */
    public static String AppPush(String orderNo) throws IOException{
        IGtPush push = new IGtPush(url, appKey, masterSecret);

        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        style.setTitle("麒麟优车");
        style.setText("来新订单了! 订单号:" + orderNo);
        style.setLogo("push.png");  // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true);  // 设置响铃
        style.setVibrate(true);  // 设置震动

        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);

        // STEP5：定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

        // STEP6：执行推送
        IPushResult ret = push.pushMessageToApp(message);
        log.info("app推送信息结果:" + ret.getResponse().toString());
        return ret.getResponse().toString();
    }

    //测试者使用
    public static void main(String[] args) throws IOException {
        String result = AppPush("YNC5FDU03840038668950_352");
        System.out.println("result--->" + result);
    }
}
